Skip to content

MkPyDeps

Show source on GitHub

Node for showing a Dependency graph.

Bases: MkNode

__init__

__init__(
    module: str | PathLike | ModuleType | None = None,
    *,
    max_bacon: int | None = None,
    max_module_depth: int | None = None,
    only_cycles: bool = False,
    clusters: bool = False,
    **kwargs: Any
)

Parameters:

Name Type Description Default
module str | PathLike | ModuleType | None

Path to the module

None
max_bacon int | None

Max bacon

None
max_module_depth int | None

Maximum module depth to display

None
only_cycles bool

Only show import cycles

False
clusters bool

draw external dependencies as separate clusters

False
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkNode
mknodes.basenodes.mknode
Base class for everything which can be expressed as Markup.
graph TD
  94721311801088["mkpydeps.MkPyDeps"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721308848336 --> 94721311801088
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/templatenodes/mkpydeps/metadata.toml
[metadata]
name = "MkPyDeps"
icon = "mdi:code-json"
group = "documentation"

# [examples.regular]
# condition = "{{ 'CI' | getenv }}"
# title = "Regular"
# jinja = """
# {{ "mknodes/data/tools.py" | MkPyDeps(max_bacon=1, max_module_depth=1) }}
# """

# [output.markdown]
# template = """
# <body>

# {{ node.svg }}

# </body>

# """
mknodes.templatenodes.mkpydeps.MkPyDeps
class MkPyDeps(mknode.MkNode):
    """Node for showing a Dependency graph."""

    ICON = "material/code-json"
    REQUIRED_PACKAGES = [resources.Package("pydeps")]

    def __init__(
        self,
        module: str | os.PathLike | types.ModuleType | None = None,
        *,
        max_bacon: int | None = None,
        max_module_depth: int | None = None,
        only_cycles: bool = False,
        clusters: bool = False,
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            module: Path to the module
            max_bacon: Max bacon
            max_module_depth: Maximum module depth to display
            only_cycles: Only show import cycles
            clusters: draw external dependencies as separate clusters
            kwargs: Keyword arguments passed to parent
        """
        self._module = module
        self.max_bacon = max_bacon
        self.max_module_depth = max_module_depth
        self.only_cycles = only_cycles
        self.clusters = clusters
        super().__init__(**kwargs)

    @property
    def module(self):
        match self._module:
            case str() | os.PathLike:
                return self._module
            case types.ModuleType():
                return inspecthelpers.get_file(self._module)
            case _:
                return self.ctx.metadata.distribution_name

    @property
    def svg(self):
        content = get_dependency_svg(
            self.module,
            max_bacon=self.max_bacon,
            max_module_depth=self.max_module_depth,
            only_cycles=self.only_cycles,
            clusters=self.clusters,
        )
        return insert_links(content, self.ctx.links.inv_manager)

    def _to_markdown(self) -> str:
        return f"<body>\n\n{self.svg}\n\n</body>\n"